import type { AssignUserParam, DeleteResponse, InheritParam, OrgTree, RoleListParam, RoleRecord, RoleUserAssignment, UserData } from '@/views/role-management/types/QueryCondition';
import * as RoleApi from '@/api/RoleManageApi';

export class RoleService {
  /**
   * 获取全部角色列表
   * @param data
   * @returns AssignRoleResponse
   */
  public static async getRoleList(data: RoleListParam) {
    try {
      const response = await RoleApi.getRoleList(data);
      return {
        code: response.code,
        data: response.data,
      };
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      data: {},
    };
  }

  /**
   * 获取已分配用户
   * @param data
   * @returns AssignRoleResponse
   */
  public static async getAssignUserList(data: AssignUserParam) {
    try {
      const response = await RoleApi.getAssignUserList(data);
      return {
        code: response.code,
        data: response.data,
      };
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      data: {},
    };
  }

  /**
   * 获取用户继承列表
   * @param data
   * @returns AssignRoleResponse
   */
  public static async getInheritRoleList(data: InheritParam) {
    try {
      const response = await RoleApi.getInheritRoleList(data);
      return {
        code: response.code,
        data: response.data,
      };
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      data: {},
    };
  }

  /**
   * 保存角色信息
   * @param data
   * @returns AssignRoleResponse
   */
  public static async saveRoleData(data: RoleRecord) {
    try {
      const response = await RoleApi.saveRoleData(data);
      return {
        code: response.code,
        message: response.message,
      };
    } catch (err: any) {
      return {
        code: '1',
        message: err.response?.data?.message || '接口调用失败',
      };
    }
  }

  /**
   * 保存角色信息
   * @param data
   * @returns AssignRoleResponse
   */
  public static async updateStatus(data: UserData) {
    try {
      const response = await RoleApi.updateStatus(data);
      return {
        code: response.code,
        message: response.message,
      };
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
    };
  }

  /**
   * 查询左侧组织机构树
   * @param data
   * @returns AssignRoleResponse
   */
  public static async queryOrgList(data: OrgTree) {
    try {
      const response = await RoleApi.queryOrgList(data);
      return {
        code: response.code,
        data: response.data,
      };
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
    };
  }

  /**
   * 保存角色信息
   * @param data
   * @returns AssignRoleResponse
   */
  public static async addAssignUser(data: RoleUserAssignment) {
    try {
      const response = await RoleApi.addAssignUser(data);
      return {
        code: response.code,
        message: response.message,
      };
    } catch (err: any) {
      return {
        code: '1',
        message: err.response?.data?.message || '接口调用失败',
      };
    }
  }

  /**
   * 保存角色信息
   * @param data
   * @returns AssignRoleResponse
   */
  public static async deleteAssignUser(data: { ids: number[] }) {
    try {
      const response = await RoleApi.deleteAssignUser(data);
      return {
        code: response.code,
        message: response.message,
      };
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
    };
  }

  /**
   * 保存角色信息
   * @param data
   * @returns AssignRoleResponse
   */
  public static async deleteRoles(data: { roleIds: number[] }): Promise<DeleteResponse> {
    try {
      const response = await RoleApi.deleteRoles(data);
      return {
        data: {
          code: response.code,
          message: response.message,
        },
        code: response.code,
        message: response.message,
      };
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      data: [],
      code: '1',
      message: '接口调用失败',
    };
  }
}
